# 项目所需常量
COMMAND_CMD_BLANK = 0x00  # 空指令

# 遥控指令
COMMAND_CTRL_RESET = 0x11  # 复位（reset）
COMMAND_CTRL_TAXI = 0x13  # 滑行
COMMAND_CTRL_BRAKING = 0x15  # 制动
COMMAND_CTRL_EMERGENCY_BRAKING = None  # 紧急制动：预停车+停车+制动组合建
COMMAND_CTRL_ANGLE_OF_TAKEOFF = 0x17  # 建立起飞角
COMMAND_CTRL_TAKE_OFF = 0x19  # 起飞
COMMAND_CTRL_NAVI = 0x1B  # 导航
COMMAND_CTRL_CLIMB_UP = 0x1D  # 爬升
COMMAND_CTRL_LEVEL_FLIGHT = 0x1F  # 平飞
COMMAND_CTRL_SLIDE = 0x31  # 下滑
COMMAND_CTRL_LAND1 = 0x33  # 着陆I
COMMAND_CTRL_LAND2 = 0x35  # 着陆II
COMMAND_CTRL_TURN_LEFT = 0x37  # 左转
COMMAND_CTRL_DIRECT_FLIGHT = 0x39  # 直飞
COMMAND_CTRL_TURN_RIGHT = 0x3B  # 右转
COMMAND_CTRL_DIRECTION = 0x3D  # 定向
COMMAND_CTRL_CIRCLE = 0x3F  # 盘旋
COMMAND_CTRL_GO_AROUND = 0x51  # 复飞
COMMAND_CTRL_CUT_IN_LOW = 0x53  # 切入低空通场
COMMAND_CTRL_CUT_OUT_LOW = 0x55  # 切出低空通场
COMMAND_CTRL_THR_TAKE_OFF = 0x71  # 起飞油门
COMMAND_CTRL_THR_IDLING = 0x73  # 怠速油门
COMMAND_CTRL_THR_CLIMB = 0x75  # 发动机爬升油门
COMMAND_CTRL_THR_CRUISE_BIG = 0x77  # 发动机巡航大油门
COMMAND_CTRL_THR_CRUISE_HALF = 0x79  # 发动机巡航中油门
COMMAND_CTRL_THR_CRUISE_SMALL = 0x7B  # 巡航小油门
COMMAND_CTRL_THR_SLIDE_BIG = 0x7D  # 发动机下滑大油门
COMMAND_CTRL_THR_SLIDE_SMALL = 0x7F  # 下滑小油门
COMMAND_CTRL_ACCESS_SPEED_CON = 0x91  # 接入速度控制
COMMAND_CTRL_DISCONNECT_SPEED_CON = 0x93  # 断开速度控制
COMMAND_CTRL_PRE_STOP = 0x95  # 预停车
COMMAND_CTRL_STOP = 0x97  # 停车
COMMAND_CTRL_LOSS_PRO_ON = 0xB1  # 上行链路丢失保护开
COMMAND_CTRL_LOSS_PRO_OFF = 0xB3  # 上行链路丢失保护关
COMMAND_CTRL_ROUTE_ON = 0xB5  # 在线航线装订开
COMMAND_CTRL_ROUTE_OFF = 0xB7  # 在线航线装订关
COMMAND_CTRL_ROUTE_PRE_DEL = 0xB9  # 航线预删除
COMMAND_CTRL_ROUTE_DEL = 0xBB  # 在线航线删除
COMMAND_CTRL_RUDDER_USE = 0xD1  # 使用方向舵
COMMAND_CTRL_RUDDER_DISABLE = 0xD3  # 禁用方向舵
COMMAND_CTRL_A_CLOSE = 0xD5  # A路闭合
COMMAND_CTRL_B_CLOSE = 0xD7  # B路闭合
COMMAND_CTRL_ALL_DISCONNECT = 0xD9  # 双路断开
COMMAND_CTRL_ALL_CLOSE = 0xDB  # 双路闭合
COMMAND_CTRL_PRESS_RUDDER_OPEN = 0xDD  # 增压舵机开
COMMAND_CTRL_PRESS_RUDDER_CLOSE = 0xDE  # 增压舵机关

COMMAND_CTRL_LINK_MASTER_POS = 0x20  # 接入主惯位置
COMMAND_CTRL_LINK_GPS_POS = 0x21  # 接入GPS位置
COMMAND_CTRL_LINK_SEC_POS = 0x22  # 接入备惯位置
COMMAND_CTRL_LINK_MASTER_ANGLE = 0x23  # 接入主惯姿态
COMMAND_CTRL_LINK_SEC_ANGLE = 0x24  # 接入备惯姿态
COMMAND_CTRL_OIL_PUMP_CLOSE = 0x25  # 辅油泵关
COMMAND_CTRL_OIL_PUMP_OPEN = 0x26  # 辅油泵开
COMMAND_CTRL_BRAKE_MAG_CLOASE = 0x27  # 刹车电磁阀关
COMMAND_CTRL_BRAKE_MAG_OPEN = 0x28  # 刹车电磁阀开
COMMAND_CTRL_OUTPUT_B_CLOSE = 0x29  # B路可控输出关/空速管加热关
COMMAND_CTRL_OUTPUT_B_OPEN = 0x2A  # B路可控输出开/空速管加热开
COMMAND_CTRL_FLAP_MOTOR_CLOSE = 0x2B  # 襟翼电机关
COMMAND_CTRL_FLAP_MOTOR_0 = 0x2C  # 襟翼电机0°
COMMAND_CTRL_FLAP_MOTOR_15 = 0x2D  # 襟翼电机15°
COMMAND_CTRL_FLAP_MOTOR_30 = 0x2E  # 襟翼电机30°
COMMAND_CTRL_FLAP_MOTOR_45 = 0x2F  # 襟翼电机45°
COMMAND_CTRL_LEFT_OIL = 0x40  # 左油箱
COMMAND_CTRL_RIGHT_OIL = 0x41  # 右油箱
COMMAND_CTRL_LINK_MASTER_ALT = 0x42  # 接入主惯高度
COMMAND_CTRL_LINK_GPS_ALT = 0x43  # 接入GPS高度
COMMAND_CTRL_LINK_AIR_PRESSURE_ALT = 0x44  # 接入气压高度
COMMAND_CTRL_MANUAL_ACCESS_RECITY_DEVIATION = 0x45  # 手动接入纠偏
COMMAND_CTRL_MANUAL_BREAK_RECITY_DEVIATION = 0x46  # 手动断开纠偏
COMMAND_CTRL_BREAK_ROLL_VEL = 0x47  # 断开滚转角速率
COMMAND_CTRL_ACCESS_ROLL_VEL = 0x48  # 接入滚转角速率
COMMAND_CTRL_BREAK_PITCH_VEL = 0x49  # 断开俯仰角速率
COMMAND_CTRL_ACCESS_PITCH_VEL = 0x4A  # 接入俯仰角速率
COMMAND_CTRL_BREAK_YAW_VEL = 0x4B  # 断开偏航角速率
COMMAND_CTRL_ACCESS_YAW_VEL = 0x4C  # 接入偏航角速率
COMMAND_CTRL_NONE_OIL = 0x4D  # 油箱关
COMMAND_CTRL_GROUND_TEST = 0x4E  # 地面测试
COMMAND_CTRL_SPEED_INT_CLOSE = 0x4F  # 速度积分断开
COMMAND_CTRL_SPEED_INT_OPEN = 0x50  # 速度积分接入
COMMAND_CTRL_RADAR_ENABLED = 0x60  # 雷达高度使能
COMMAND_CTRL_RADAR_DISABLED = 0x61  # 雷达高度禁止
COMMAND_CTRL_VI_FILTER_ENABLED = 0x62  # 空速滤波开
COMMAND_CTRL_VI_FILTER_DISABLED = 0x63  # 空速滤波关
COMMAND_CTRL_ALTER1 = 0x64  # 备用1
COMMAND_CTRL_ALTER2 = 0x65  # 备用2
COMMAND_CTRL_ALTER3 = 0x66  # 备用3
COMMAND_CTRL_ALTER4 = 0x67  # 备用4
COMMAND_CTRL_ALTER5 = 0x68  # 备用5
COMMAND_CTRL_ALTER6 = 0x69  # 备用6

BACK_CTRL_RESET = 1  # 复位（reset）
BACK_CTRL_TAXI = 2  # 滑行
BACK_CTRL_BRAKING = 3  # 制动
# BACK_CTRL_EMERGENCY_BRAKING = 4  # 紧急制动：预停车+停车+制动组合建
BACK_CTRL_ANGLE_OF_TAKEOFF = 4  # 建立起飞角
BACK_CTRL_TAKE_OFF = 5  # 起飞
BACK_CTRL_NAVI = 6  # 导航
BACK_CTRL_CLIMB_UP = 7  # 爬升
BACK_CTRL_LEVEL_FLIGHT = 8  # 平飞
BACK_CTRL_SLIDE = 9  # 下滑
BACK_CTRL_LAND1 = 10  # 着陆I
BACK_CTRL_LAND2 = 11  # 着陆II
BACK_CTRL_TURN_LEFT = 12  # 左转
BACK_CTRL_DIRECT_FLIGHT = 13  # 直飞
BACK_CTRL_TURN_RIGHT = 14  # 右转
BACK_CTRL_DIRECTION = 15  # 定向
BACK_CTRL_CIRCLE = 16  # 盘旋
BACK_CTRL_GO_AROUND = 17  # 复飞
BACK_CTRL_CUT_IN_LOW = 18  # 切入低空通场
BACK_CTRL_CUT_OUT_LOW = 19  # 切出低空通场
BACK_CTRL_THR_TAKE_OFF = 20  # 起飞油门
BACK_CTRL_THR_IDLING = 21  # 怠速油门
BACK_CTRL_THR_CLIMB = 22  # 发动机爬升油门
BACK_CTRL_THR_CRUISE_BIG = 23  # 发动机巡航大油门
BACK_CTRL_THR_CRUISE_HALF = 24  # 发动机巡航中油门
BACK_CTRL_THR_CRUISE_SMALL = 25  # 巡航小油门
BACK_CTRL_THR_SLIDE_BIG = 26  # 发动机下滑大油门
BACK_CTRL_THR_SLIDE_SMALL = 27  # 下滑小油门
BACK_CTRL_ACCESS_SPEED_CON = 28  # 接入速度控制
BACK_CTRL_DISCONNECT_SPEED_CON = 29  # 断开速度控制
BACK_CTRL_PRE_STOP = 30  # 预停车
BACK_CTRL_STOP = 31  # 停车
BACK_CTRL_LOSS_PRO_ON = 32  # 上行链路丢失保护开
BACK_CTRL_LOSS_PRO_OFF = 33  # 上行链路丢失保护关
BACK_CTRL_ROUTE_ON = 34  # 在线航线装订开
BACK_CTRL_ROUTE_OFF = 35  # 在线航线装订关
BACK_CTRL_ROUTE_PRE_DEL = 36  # 航线预删除
BACK_CTRL_ROUTE_DEL = 37  # 在线航线删除
BACK_CTRL_RUDDER_USE = 38  # 使用方向舵
BACK_CTRL_RUDDER_DISABLE = 39  # 禁用方向舵
BACK_CTRL_A_CLOSE = 40  # A路闭合
BACK_CTRL_B_CLOSE = 41  # B路闭合
BACK_CTRL_ALL_DISCONNECT = 42  # 双路断开
BACK_CTRL_ALL_CLOSE = 43  # 双路闭合
BACK_CTRL_PRESS_RUDDER_OPEN = 44  # 增压舵机开
BACK_CTRL_PRESS_RUDDER_CLOSE = 45  # 增压舵机关
BACK_CTRL_LINK_MASTER_POS = 46  # 接入主惯位置
BACK_CTRL_LINK_GPS_POS = 47  # 接入GPS位置
BACK_CTRL_LINK_SEC_POS = 48  # 接入备惯位置
BACK_CTRL_LINK_MASTER_ANGLE = 49  # 接入主惯姿态
BACK_CTRL_LINK_SEC_ANGLE = 50  # 接入备惯姿态
BACK_CTRL_OIL_PUMP_CLOSE = 51  # 辅油泵关
BACK_CTRL_OIL_PUMP_OPEN = 52  # 辅油泵开
BACK_CTRL_BRAKE_MAG_CLOASE = 53  # 刹车电磁阀关
BACK_CTRL_BRAKE_MAG_OPEN = 54  # 刹车电磁阀开
BACK_CTRL_OUTPUT_B_CLOSE = 55  # B路可控输出关/空速管加热关
BACK_CTRL_OUTPUT_B_OPEN = 56  # B路可控输出开/空速管加热开
BACK_CTRL_FLAP_MOTOR_CLOSE = 57  # 襟翼电机关
BACK_CTRL_FLAP_MOTOR_0 = 58  # 襟翼电机0°
BACK_CTRL_FLAP_MOTOR_15 = 59  # 襟翼电机15°
BACK_CTRL_FLAP_MOTOR_30 = 60  # 襟翼电机30°
BACK_CTRL_FLAP_MOTOR_45 = 61  # 襟翼电机45°
BACK_CTRL_LEFT_OIL = 62  # 左油箱
BACK_CTRL_RIGHT_OIL = 63  # 右油箱
BACK_CTRL_LINK_MASTER_ALT = 64  # 接入主惯高度
BACK_CTRL_LINK_GPS_ALT = 65  # 接入GPS高度
BACK_CTRL_LINK_AIR_PRESSURE_ALT = 66  # 接入气压高度
BACK_CTRL_MANUAL_ACCESS_RECITY_DEVIATION = 67  # 手动接入纠偏
BACK_CTRL_MANUAL_BREAK_RECITY_DEVIATION = 68  # 手动断开纠偏
BACK_CTRL_BREAK_ROLL_VEL = 69  # 断开滚转角速率
BACK_CTRL_ACCESS_ROLL_VEL = 70  # 接入滚转角速率
BACK_CTRL_BREAK_PITCH_VEL = 71  # 断开俯仰角速率
BACK_CTRL_ACCESS_PITCH_VEL = 72  # 接入俯仰角速率
BACK_CTRL_BREAK_YAW_VEL = 73  # 断开偏航角速率
BACK_CTRL_ACCESS_YAW_VEL = 74  # 接入偏航角速率
BACK_CTRL_NONE_OIL = 75  # 油箱关
BACK_CTRL_GROUND_TEST = 76  # 地面测试
BACK_CTRL_SPEED_INT_CLOSE = 77  # 速度积分断开
BACK_CTRL_SPEED_INT_OPEN = 78  # 速度积分接入
BACK_CTRL_RADAR_ENABLED = 79  # 雷达高度使能
BACK_CTRL_RADAR_DISABLED = 80  # 雷达高度禁止
BACK_CTRL_VI_FILTER_ENABLED = 81  # 空速滤波开
BACK_CTRL_VI_FILTER_DISABLED = 82  # 空速滤波关
BACK_CTRL_ALTER1 = 83  # 备用1
BACK_CTRL_ALTER2 = 84  # 备用2
BACK_CTRL_ALTER3 = 85  # 备用3
BACK_CTRL_ALTER4 = 86  # 备用4
BACK_CTRL_ALTER5 = 87  # 备用5
BACK_CTRL_ALTER6 = 88  # 备用6
CTRL_COMMAND_BACK_DICT = {
    COMMAND_CTRL_RESET: BACK_CTRL_RESET,
    COMMAND_CTRL_TAXI: BACK_CTRL_TAXI,
    COMMAND_CTRL_BRAKING: BACK_CTRL_BRAKING,
    COMMAND_CTRL_ANGLE_OF_TAKEOFF: BACK_CTRL_ANGLE_OF_TAKEOFF,
    COMMAND_CTRL_TAKE_OFF: BACK_CTRL_TAKE_OFF,
    COMMAND_CTRL_NAVI: BACK_CTRL_NAVI,
    COMMAND_CTRL_CLIMB_UP: BACK_CTRL_CLIMB_UP,
    COMMAND_CTRL_LEVEL_FLIGHT: BACK_CTRL_LEVEL_FLIGHT,
    COMMAND_CTRL_SLIDE: BACK_CTRL_SLIDE,
    COMMAND_CTRL_LAND1: BACK_CTRL_LAND1,
    COMMAND_CTRL_LAND2: BACK_CTRL_LAND2,
    COMMAND_CTRL_TURN_LEFT: BACK_CTRL_TURN_LEFT,
    COMMAND_CTRL_DIRECT_FLIGHT: BACK_CTRL_DIRECT_FLIGHT,
    COMMAND_CTRL_TURN_RIGHT: BACK_CTRL_TURN_RIGHT,
    COMMAND_CTRL_DIRECTION: BACK_CTRL_DIRECTION,
    COMMAND_CTRL_CIRCLE: BACK_CTRL_CIRCLE,
    COMMAND_CTRL_GO_AROUND: BACK_CTRL_GO_AROUND,
    COMMAND_CTRL_CUT_IN_LOW: BACK_CTRL_CUT_IN_LOW,
    COMMAND_CTRL_CUT_OUT_LOW: BACK_CTRL_CUT_OUT_LOW,
    COMMAND_CTRL_THR_TAKE_OFF: BACK_CTRL_THR_TAKE_OFF,
    COMMAND_CTRL_THR_IDLING: BACK_CTRL_THR_IDLING,
    COMMAND_CTRL_THR_CLIMB: BACK_CTRL_THR_CLIMB,
    COMMAND_CTRL_THR_CRUISE_BIG: BACK_CTRL_THR_CRUISE_BIG,
    COMMAND_CTRL_THR_CRUISE_HALF: BACK_CTRL_THR_CRUISE_HALF,
    COMMAND_CTRL_THR_CRUISE_SMALL: BACK_CTRL_THR_CRUISE_SMALL,
    COMMAND_CTRL_THR_SLIDE_BIG: BACK_CTRL_THR_SLIDE_BIG,
    COMMAND_CTRL_THR_SLIDE_SMALL: BACK_CTRL_THR_SLIDE_SMALL,
    COMMAND_CTRL_ACCESS_SPEED_CON: BACK_CTRL_ACCESS_SPEED_CON,
    COMMAND_CTRL_DISCONNECT_SPEED_CON: BACK_CTRL_DISCONNECT_SPEED_CON,
    COMMAND_CTRL_PRE_STOP: BACK_CTRL_PRE_STOP,
    COMMAND_CTRL_STOP: BACK_CTRL_STOP,
    COMMAND_CTRL_LOSS_PRO_ON: BACK_CTRL_LOSS_PRO_ON,
    COMMAND_CTRL_LOSS_PRO_OFF: BACK_CTRL_LOSS_PRO_OFF,
    COMMAND_CTRL_ROUTE_ON: BACK_CTRL_ROUTE_ON,
    COMMAND_CTRL_ROUTE_OFF: BACK_CTRL_ROUTE_OFF,
    COMMAND_CTRL_ROUTE_PRE_DEL: BACK_CTRL_ROUTE_PRE_DEL,
    COMMAND_CTRL_ROUTE_DEL: BACK_CTRL_ROUTE_DEL,
    COMMAND_CTRL_RUDDER_USE: BACK_CTRL_RUDDER_USE,
    COMMAND_CTRL_RUDDER_DISABLE: BACK_CTRL_RUDDER_DISABLE,
    COMMAND_CTRL_A_CLOSE: BACK_CTRL_A_CLOSE,
    COMMAND_CTRL_B_CLOSE: BACK_CTRL_B_CLOSE,
    COMMAND_CTRL_ALL_DISCONNECT: BACK_CTRL_ALL_DISCONNECT,
    COMMAND_CTRL_ALL_CLOSE: BACK_CTRL_ALL_CLOSE,
    COMMAND_CTRL_PRESS_RUDDER_OPEN: BACK_CTRL_PRESS_RUDDER_OPEN,
    COMMAND_CTRL_PRESS_RUDDER_CLOSE: BACK_CTRL_PRESS_RUDDER_CLOSE,
    COMMAND_CTRL_LINK_MASTER_POS: BACK_CTRL_LINK_MASTER_POS,
    COMMAND_CTRL_LINK_GPS_POS: BACK_CTRL_LINK_GPS_POS,
    COMMAND_CTRL_LINK_SEC_POS: BACK_CTRL_LINK_SEC_POS,
    COMMAND_CTRL_LINK_MASTER_ANGLE: BACK_CTRL_LINK_MASTER_ANGLE,
    COMMAND_CTRL_LINK_SEC_ANGLE: BACK_CTRL_LINK_SEC_ANGLE,
    COMMAND_CTRL_OIL_PUMP_CLOSE: BACK_CTRL_OIL_PUMP_CLOSE,
    COMMAND_CTRL_OIL_PUMP_OPEN: BACK_CTRL_OIL_PUMP_OPEN,
    COMMAND_CTRL_BRAKE_MAG_CLOASE: BACK_CTRL_BRAKE_MAG_CLOASE,
    COMMAND_CTRL_BRAKE_MAG_OPEN: BACK_CTRL_BRAKE_MAG_OPEN,
    COMMAND_CTRL_OUTPUT_B_CLOSE: BACK_CTRL_OUTPUT_B_CLOSE,
    COMMAND_CTRL_OUTPUT_B_OPEN: BACK_CTRL_OUTPUT_B_OPEN,
    COMMAND_CTRL_FLAP_MOTOR_CLOSE: BACK_CTRL_FLAP_MOTOR_CLOSE,
    COMMAND_CTRL_FLAP_MOTOR_0: BACK_CTRL_FLAP_MOTOR_0,
    COMMAND_CTRL_FLAP_MOTOR_15: BACK_CTRL_FLAP_MOTOR_15,
    COMMAND_CTRL_FLAP_MOTOR_30: BACK_CTRL_FLAP_MOTOR_30,
    COMMAND_CTRL_FLAP_MOTOR_45: BACK_CTRL_FLAP_MOTOR_45,
    COMMAND_CTRL_LEFT_OIL: BACK_CTRL_LEFT_OIL,
    COMMAND_CTRL_RIGHT_OIL: BACK_CTRL_RIGHT_OIL,
    COMMAND_CTRL_LINK_MASTER_ALT: BACK_CTRL_LINK_MASTER_ALT,
    COMMAND_CTRL_LINK_GPS_ALT: BACK_CTRL_LINK_GPS_ALT,
    COMMAND_CTRL_LINK_AIR_PRESSURE_ALT: BACK_CTRL_LINK_AIR_PRESSURE_ALT,
    COMMAND_CTRL_MANUAL_ACCESS_RECITY_DEVIATION: BACK_CTRL_MANUAL_ACCESS_RECITY_DEVIATION,
    COMMAND_CTRL_MANUAL_BREAK_RECITY_DEVIATION: BACK_CTRL_MANUAL_BREAK_RECITY_DEVIATION,
    COMMAND_CTRL_BREAK_ROLL_VEL: BACK_CTRL_BREAK_ROLL_VEL,
    COMMAND_CTRL_ACCESS_ROLL_VEL: BACK_CTRL_ACCESS_ROLL_VEL,
    COMMAND_CTRL_BREAK_PITCH_VEL: BACK_CTRL_BREAK_PITCH_VEL,
    COMMAND_CTRL_ACCESS_PITCH_VEL: BACK_CTRL_ACCESS_PITCH_VEL,
    COMMAND_CTRL_BREAK_YAW_VEL: BACK_CTRL_BREAK_YAW_VEL,
    COMMAND_CTRL_ACCESS_YAW_VEL: BACK_CTRL_ACCESS_YAW_VEL,
    COMMAND_CTRL_NONE_OIL: BACK_CTRL_NONE_OIL,
    COMMAND_CTRL_GROUND_TEST: BACK_CTRL_GROUND_TEST,
    COMMAND_CTRL_SPEED_INT_CLOSE: BACK_CTRL_SPEED_INT_CLOSE,
    COMMAND_CTRL_SPEED_INT_OPEN: BACK_CTRL_SPEED_INT_OPEN,
    COMMAND_CTRL_RADAR_ENABLED: BACK_CTRL_RADAR_ENABLED,
    COMMAND_CTRL_RADAR_DISABLED: BACK_CTRL_RADAR_DISABLED,
    COMMAND_CTRL_VI_FILTER_ENABLED: BACK_CTRL_VI_FILTER_ENABLED,
    COMMAND_CTRL_VI_FILTER_DISABLED: BACK_CTRL_VI_FILTER_DISABLED,
    COMMAND_CTRL_ALTER1: BACK_CTRL_ALTER1,
    COMMAND_CTRL_ALTER2: BACK_CTRL_ALTER2,
    COMMAND_CTRL_ALTER3: BACK_CTRL_ALTER3,
    COMMAND_CTRL_ALTER4: BACK_CTRL_ALTER4,
    COMMAND_CTRL_ALTER5: BACK_CTRL_ALTER5,
    COMMAND_CTRL_ALTER6: BACK_CTRL_ALTER6
}
# 遥调指令，对应指令号，1，2，3。。。。
COMMAND_ADJUST_ROUTE_NUM = 0x01  # 航点号遥调
COMMAND_ADJUST_ROUTE_DIRECT = 0x02  # 航向遥调
COMMAND_ADJUST_PITCH = 0x03  # 俯仰角遥调
COMMAND_ADJUST_ROLL = 0x04  # 滚转角遥调
COMMAND_ADJUST_YAW = 0x05  # 侧偏遥调
COMMAND_ADJUST_THR = 0x06  # 油门遥调
COMMAND_ADJUST_FIELD_HEIGHT = 0x07  # 场高遥调
COMMAND_ADJUST_BACK_WHEEL_DIFF = 0x08  # 后轮差动量
COMMAND_ADJUST_BACK_WHEEL_BRAKE = 0x09  # 后轮制动量
COMMAND_ADJUST_ROUTE_ALT = 0x0a  # 航路高度遥调
COMMAND_ADJUST_LOAD = 0x0b  # 载荷
COMMAND_ADJUST_TRACE_ANGLE = 0x0c  # 陡下滑轨迹角遥调
COMMAND_ADJUST_AIR_SPEED = 0x0d  # 空速遥调
COMMAND_ADJUST_ROUTE_SET = 0x0e  # 在线航线上传
COMMAND_ADJUST_ROUTE_BACK = 0x0f  # 在线航线下载
COMMAND_ADJUST_ROUTE_ENTER = 0x10  # 在线航点切入
COMMAND_ADJUST_LOWSPACE_FLY_ALT = 0x11  # 低空飞行高度遥调
COMMAND_ADJUST_LIFT_RUDDER = 0x12  # 升降舵遥调
COMMAND_ADJUST_AILERON_RUDDER = 0x13  # 副翼舵遥调
COMMAND_ADJUST_COURSE_RUDDER = 0x14  # 方向舵遥调
COMMAND_ADJUST_TAXI_IN_ROUTE_UPLOAD = 0x15  # 驶入航线上传
COMMAND_ADJUST_TAXI_IN_ROUTE_DOWNLOAD = 0x16  # 驶入航线下载
COMMAND_ADJUST_TAXI_IN = 0x17  # 跑道驶入
COMMAND_ADJUST_TAXI_OUT_ROUTE_UPLOAD = 0x18  # 跑道驶出航线上传
COMMAND_ADJUST_TAXI_OUT_ROUTE_DOWNLOAD = 0x19  # 跑道驶出航线下载
COMMAND_ADJUST_TAXI_OUT = 0x1a  # 跑道驶出
COMMAND_ADJUST_FRONT_WHEEL_DEG = 0x1b  # 前轮偏转角遥调
COMMAND_ADJUST_ADC2_HEATING = 0x1c  # 空速管2加热控制
COMMAND_ADJUST_ADC2_VIAS_RESET = 0x1d  # 空速管2空速清零
COMMAND_ADJUST_ADC2_AOA_RESET = 0x1e  # 空速管2迎角清零

# 遥调指令，指令回馈
BACK_ADJUST_ROUTE_NUM = 0xF1
BACK_ADJUST_ROUTE_DIRECT = 0xF2
BACK_ADJUST_PITCH = 0xF3
BACK_ADJUST_ROLL = 0xF4
BACK_ADJUST_YAW = 0xF5
BACK_ADJUST_THR = 0xF6
BACK_ADJUST_FIELD_HEIGHT = 0xF7
BACK_ADJUST_BACK_WHEEL_DIFF = 0xF8
BACK_ADJUST_BACK_WHEEL_BRAKE = 0xF9
BACK_ADJUST_ROUTE_ALT = 0xFA
BACK_ADJUST_LOAD = 0xFB
BACK_ADJUST_TRACE_ANGLE = 0xFC
BACK_ADJUST_AIR_SPEED = 0xFD
BACK_ADJUST_ROUTE_SET = 0xFE
BACK_ADJUST_ROUTE_BACK = 0xFF
BACK_ADJUST_ROUTE_ENTER = 0xF0
BACK_ADJUST_LOWSPACE_FLY_ALT = 0xE0
BACK_ADJUST_LIFT_RUDDER = 0xE1  # 升降舵遥调
BACK_ADJUST_AILERON_RUDDER = 0xE2  # 副翼舵遥调
BACK_ADJUST_COURSE_RUDDER = 0xE3  # 方向舵遥调
BACK_ADJUST_TAXI_IN_ROUTE_UPLOAD = 0xE4  # 驶入航线上传
BACK_ADJUST_TAXI_IN_ROUTE_DOWNLOAD = 0xE5  # 驶入航线下载
BACK_ADJUST_TAXI_IN = 0xE6  # 跑道驶入
BACK_ADJUST_TAXI_OUT_ROUTE_UPLOAD = 0xE7  # 跑道驶出航线上传
BACK_ADJUST_TAXI_OUT_ROUTE_DOWNLOAD = 0xE8  # 跑道驶出航线下载
BACK_ADJUST_TAXI_OUT = 0xE9  # 跑道驶出
BACK_ADJUST_FRONT_WHEEL_DEG = 0xEA  # 前轮偏转角遥调
BACK_ADJUST_ADC2_HEATING = 0xD0  # 空速管2加热控制
BACK_ADJUST_ADC2_VIAS_RESET = 0xD1  # 空速管2空速清零
BACK_ADJUST_ADC2_AOA_RESET = 0xD2  # 空速管2迎角清零

# 遥调指令回传的命令与遥调指令的一一对应，数组下标是指令编号，具体值为对应的指令回复
ADJUST_ECHO_CODE_LIST = [COMMAND_CMD_BLANK,
                         BACK_ADJUST_ROUTE_NUM,
                         BACK_ADJUST_ROUTE_DIRECT,
                         BACK_ADJUST_PITCH,
                         BACK_ADJUST_ROLL,
                         BACK_ADJUST_YAW,
                         BACK_ADJUST_THR,
                         BACK_ADJUST_FIELD_HEIGHT,
                         BACK_ADJUST_BACK_WHEEL_DIFF,
                         BACK_ADJUST_BACK_WHEEL_BRAKE,
                         BACK_ADJUST_ROUTE_ALT,
                         BACK_ADJUST_LOAD,
                         BACK_ADJUST_TRACE_ANGLE,
                         BACK_ADJUST_AIR_SPEED,
                         BACK_ADJUST_ROUTE_SET,
                         BACK_ADJUST_ROUTE_BACK,
                         BACK_ADJUST_ROUTE_ENTER,
                         BACK_ADJUST_LOWSPACE_FLY_ALT,
                         BACK_ADJUST_LIFT_RUDDER,
                         BACK_ADJUST_AILERON_RUDDER,
                         BACK_ADJUST_COURSE_RUDDER,
                         BACK_ADJUST_TAXI_IN_ROUTE_UPLOAD,
                         BACK_ADJUST_TAXI_IN_ROUTE_DOWNLOAD,
                         BACK_ADJUST_TAXI_IN,
                         BACK_ADJUST_TAXI_OUT_ROUTE_UPLOAD,
                         BACK_ADJUST_TAXI_OUT_ROUTE_DOWNLOAD,
                         BACK_ADJUST_TAXI_OUT,
                         BACK_ADJUST_FRONT_WHEEL_DEG,
                         BACK_ADJUST_ADC2_HEATING,
                         BACK_ADJUST_ADC2_VIAS_RESET,
                         BACK_ADJUST_ADC2_AOA_RESET]

# 数据长度0x40 64
RECEIVE_DATA_LEN = 64


# 数据尾校验和的计算
def checknum(data, count):
    s = 0
    for i in range(0, count):
        s = s + data[i]

    s = s & 0xFF

    return s


# 数值转为二进制字符串
def bin_str(value):
    s = bin(value)
    s = s[2:]
    for i in range(8 - len(s)):
        s = '0' + s
    return s


# 封装航点
class RouteInfo:
    def __init__(self):
        self.no = 0  # 序号
        self.lat = 0  # 纬度
        self.lon = 0  # 经度
        self.alt = 0  # 高度
        self.spd = 0  # 速度
        self.status = 0  # 状态字
        self.parm = 0  # 任务参数
        self.radious = 0 # 转弯半径-仅离地面滑行航线需要


# 封装连接状态
class ConnectStatus:
    Scanning = 0  # 扫描
    Connected = 1  # 连接
    Disconnected = 2  # 断连


# 区分航点指令来源
class SendRouteSource:
    Config = 0  # 地检触发上传航路点
    Adjust = 1  # 遥调触发上传航路点


# 区分app类型
class AppType:
    Inspect = 0  # 地检
    GCS = 1  # 遥控遥调


# 区分命令类型
class CommandType:
    Control = 0  # 遥控
    Adjust = 1  # 遥调
    Config = 2  # 地检
    Unknown = 3  # 未知


COMMAND_NAME_DICT = {
    CommandType.Control: {
        COMMAND_CTRL_RESET: '复位',
        COMMAND_CTRL_TAXI: '滑行',
        COMMAND_CTRL_BRAKING: '制动',
        COMMAND_CTRL_ANGLE_OF_TAKEOFF: '预起飞',
        COMMAND_CTRL_TAKE_OFF: '起飞',
        COMMAND_CTRL_NAVI: '导航',
        COMMAND_CTRL_CLIMB_UP: '爬升',
        COMMAND_CTRL_LEVEL_FLIGHT: '平飞',
        COMMAND_CTRL_SLIDE: '下滑',
        COMMAND_CTRL_LAND1: '着陆I',
        COMMAND_CTRL_LAND2: '着陆II',
        COMMAND_CTRL_TURN_LEFT: '左转',
        COMMAND_CTRL_DIRECT_FLIGHT: '直飞',
        COMMAND_CTRL_TURN_RIGHT: '右转',
        COMMAND_CTRL_DIRECTION: '定向',
        COMMAND_CTRL_CIRCLE: '盘旋',
        COMMAND_CTRL_GO_AROUND: '复飞',
        COMMAND_CTRL_CUT_IN_LOW: '切入低空通场',
        COMMAND_CTRL_CUT_OUT_LOW: '切出低空通场',
        COMMAND_CTRL_THR_TAKE_OFF: '起飞油门',
        COMMAND_CTRL_THR_IDLING: '怠速油门',
        COMMAND_CTRL_THR_CLIMB: '发动机爬升油门',
        COMMAND_CTRL_THR_CRUISE_BIG: '发动机巡航大油门',
        COMMAND_CTRL_THR_CRUISE_HALF: '发动机巡航中油门',
        COMMAND_CTRL_THR_CRUISE_SMALL: '巡航小油门',
        COMMAND_CTRL_THR_SLIDE_BIG: '发动机下滑大油门',
        COMMAND_CTRL_THR_SLIDE_SMALL: '下滑小油门',
        COMMAND_CTRL_ACCESS_SPEED_CON: '接入速度控制',
        COMMAND_CTRL_DISCONNECT_SPEED_CON: '断开速度控制',
        COMMAND_CTRL_PRE_STOP: '预停车',
        COMMAND_CTRL_STOP: '停车',
        COMMAND_CTRL_LOSS_PRO_ON: '上行链路丢失保护开',
        COMMAND_CTRL_LOSS_PRO_OFF: '上行链路丢失保护关',
        COMMAND_CTRL_ROUTE_ON: '在线航线装订开',
        COMMAND_CTRL_ROUTE_OFF: '在线航线装订关',
        COMMAND_CTRL_ROUTE_PRE_DEL: '航线预删除',
        COMMAND_CTRL_ROUTE_DEL: '在线航线删除',
        COMMAND_CTRL_RUDDER_USE: '使用方向舵',
        COMMAND_CTRL_RUDDER_DISABLE: '禁用方向舵',
        COMMAND_CTRL_A_CLOSE: 'A路关',
        COMMAND_CTRL_B_CLOSE: 'B路关',
        COMMAND_CTRL_ALL_DISCONNECT: 'AB路开',
        COMMAND_CTRL_ALL_CLOSE: 'AB路关',
        COMMAND_CTRL_PRESS_RUDDER_OPEN: '增压舵机开',
        COMMAND_CTRL_PRESS_RUDDER_CLOSE: '增压舵机关',
        COMMAND_CTRL_LINK_MASTER_POS: '接入主惯位置',
        COMMAND_CTRL_LINK_GPS_POS: '接入GPS位置',
        COMMAND_CTRL_LINK_SEC_POS: '接入备惯位置',
        COMMAND_CTRL_LINK_MASTER_ANGLE: '接入主惯姿态',
        COMMAND_CTRL_LINK_SEC_ANGLE: '接入备惯姿态',
        COMMAND_CTRL_OIL_PUMP_CLOSE: '辅油泵关',
        COMMAND_CTRL_OIL_PUMP_OPEN: '辅油泵开',
        COMMAND_CTRL_BRAKE_MAG_CLOASE: '刹车电磁阀关',
        COMMAND_CTRL_BRAKE_MAG_OPEN: '刹车电磁阀开',
        COMMAND_CTRL_OUTPUT_B_CLOSE: '空速管加热关',  # 'B路可控输出关'
        COMMAND_CTRL_OUTPUT_B_OPEN: '空速管加热开',  # 'B路可控输出开'
        COMMAND_CTRL_FLAP_MOTOR_CLOSE: '襟翼电机关',
        COMMAND_CTRL_FLAP_MOTOR_0: '襟翼0°',
        COMMAND_CTRL_FLAP_MOTOR_15: '襟翼15°',
        COMMAND_CTRL_FLAP_MOTOR_30: '襟翼30°',
        COMMAND_CTRL_FLAP_MOTOR_45: '襟翼45°',
        COMMAND_CTRL_LEFT_OIL: '左油箱',
        COMMAND_CTRL_RIGHT_OIL: '右油箱',
        COMMAND_CTRL_NONE_OIL: '油箱关',
        COMMAND_CTRL_LINK_MASTER_ALT: '接入主惯高度',
        COMMAND_CTRL_LINK_GPS_ALT: '接入GPS高度',
        COMMAND_CTRL_LINK_AIR_PRESSURE_ALT: '接入气压高度',
        COMMAND_CTRL_MANUAL_ACCESS_RECITY_DEVIATION: '手动接入纠偏',
        COMMAND_CTRL_MANUAL_BREAK_RECITY_DEVIATION: '手动断开纠偏',
        COMMAND_CTRL_BREAK_ROLL_VEL: '断开滚转角速率',
        COMMAND_CTRL_ACCESS_ROLL_VEL: '接入滚转角速率',
        COMMAND_CTRL_BREAK_PITCH_VEL: '断开俯仰角速率',
        COMMAND_CTRL_ACCESS_PITCH_VEL: '接入俯仰角速率',
        COMMAND_CTRL_BREAK_YAW_VEL: '断开偏航角速率',
        COMMAND_CTRL_ACCESS_YAW_VEL: '接入偏航角速率',
        COMMAND_CTRL_GROUND_TEST: '地面测试',
        COMMAND_CTRL_SPEED_INT_CLOSE: '速度积分断开',
        COMMAND_CTRL_SPEED_INT_OPEN: '速度积分接入',
        COMMAND_CTRL_RADAR_ENABLED: '雷达高度使能',
        COMMAND_CTRL_RADAR_DISABLED: '雷达高度禁止',
        COMMAND_CTRL_VI_FILTER_ENABLED: '空速滤波开',
        COMMAND_CTRL_VI_FILTER_DISABLED: '空速滤波关',
        COMMAND_CTRL_ALTER1: '备用1',
        COMMAND_CTRL_ALTER2: '备用2',
        COMMAND_CTRL_ALTER3: '备用3',
        COMMAND_CTRL_ALTER4: '备用4',
        COMMAND_CTRL_ALTER5: '备用5',
        COMMAND_CTRL_ALTER6: '备用6'
        # TODO:查询跑道参数
        # TODO:删除跑道参数
    },
    CommandType.Adjust: {
        COMMAND_ADJUST_ROUTE_NUM: '航点号遥调',
        COMMAND_ADJUST_ROUTE_DIRECT: '航向遥调',
        COMMAND_ADJUST_PITCH: '俯仰角遥调',
        COMMAND_ADJUST_ROLL: '滚转角遥调',
        COMMAND_ADJUST_YAW: '侧偏遥调',
        COMMAND_ADJUST_THR: '油门遥调',
        COMMAND_ADJUST_FIELD_HEIGHT: '场高遥调',
        COMMAND_ADJUST_BACK_WHEEL_DIFF: '后轮差动遥调',
        COMMAND_ADJUST_BACK_WHEEL_BRAKE: '后轮制动遥调',
        COMMAND_ADJUST_ROUTE_ALT: '航路高度遥调',
        COMMAND_ADJUST_LOAD: '载荷遥调',
        COMMAND_ADJUST_TRACE_ANGLE: '陡下滑轨迹角遥调',
        COMMAND_ADJUST_AIR_SPEED: '空速遥调',
        COMMAND_ADJUST_ROUTE_SET: '在线航线上传',
        COMMAND_ADJUST_ROUTE_BACK: '在线航线下载',
        COMMAND_ADJUST_ROUTE_ENTER: '在线航点切入',
        COMMAND_ADJUST_LOWSPACE_FLY_ALT: '低空飞行高度遥调',
        COMMAND_ADJUST_LIFT_RUDDER: '升降舵遥调',
        COMMAND_ADJUST_AILERON_RUDDER: '副翼舵遥调',
        COMMAND_ADJUST_COURSE_RUDDER: '方向舵遥调',
        COMMAND_ADJUST_TAXI_IN_ROUTE_UPLOAD: '驶入航线上传',
        COMMAND_ADJUST_TAXI_IN_ROUTE_DOWNLOAD: '驶入航线下载',
        COMMAND_ADJUST_TAXI_IN: '跑道驶入',
        COMMAND_ADJUST_TAXI_OUT_ROUTE_UPLOAD: '跑道驶出航线上传',
        COMMAND_ADJUST_TAXI_OUT_ROUTE_DOWNLOAD: '跑道驶出航线下载',
        COMMAND_ADJUST_TAXI_OUT: '跑道驶出',
        COMMAND_ADJUST_FRONT_WHEEL_DEG: '前轮偏转遥调',
        COMMAND_ADJUST_ADC2_HEATING: '空速管2加热',
        COMMAND_ADJUST_ADC2_VIAS_RESET: '空速管2空速清零',
        COMMAND_ADJUST_ADC2_AOA_RESET: '空速管2风标重置',
        # TODO:起飞跑道装订
        # TODO:着陆跑道装订
        # TODO:着陆跑道装订
    }
}

# 配置文件相关常量
# config，跑道设置标题
FILE_LEVEL_RUNWAY = 'Runway_Setting'  # 跑道设置
FILE_ITEM_RUNWAY_START_LAT = 'start_lat'  # 起点纬度
FILE_ITEM_RUNWAY_START_LON = 'start_lon'  # 起点经度
FILE_ITEM_RUNWAY_END_LAT = 'end_lat'  # 终点纬度
FILE_ITEM_RUNWAY_END_LON = 'end_lon'  # 终点经度
FILE_ITEM_RUNWAY_WIDTH = 'width'  # 跑道宽度

CFG_NODE_TAKEOFF_RWY = 'Takeoff_Runway'  # 跑道设置
CFG_NODE_LANDING_RWY = 'Landing_Runway'  # 跑道设置
CFG_ITEM_RWY_APT_CODE = 'apt_code'  # 跑道所在机场代号
CFG_ITEM_RWY_ID = 'rwy_id'  # 跑道ID
CFG_ITEM_RWY_START_LAT = 'start_lat'  # 起点纬度
CFG_ITEM_RWY_START_LON = 'start_lon'  # 起点经度
CFG_ITEM_RWY_END_LAT = 'end_lat'  # 终点纬度
CFG_ITEM_RWY_END_LON = 'end_lon'  # 终点经度
CFG_ITEM_RWY_HEADING = 'rwy_heading'  # 跑道航向
CFG_ITEM_RWY_ALT = 'rwy_alt'  # 跑道高度
CFG_ITEM_RWY_LEN = 'rwy_slop'  # 跑道坡度
CFG_ITEM_RWY_WIDTH = 'rwy_width'  # 跑道宽度

DEFAULT_RWY_WIDTH = 30

MSG_REQUEST_TIMEOUT = '请求超时！'
MSG_REQUEST_DISCONNECT = '飞控未连接！'


NODE_SETTING = "MQTT_SETTING"
ITEM_MQTT_SERVER_IP = "mqtt_broker_address"
ITEM_MQTT_PORT = "mqtt_port"

NODE_PUB_TOPIC = "PUBLISH_TOPIC"
ITEM_COMMAND_SEND = "command_send" # 目标主题

NODE_SUB_TOPIC = "SUBSCRIBE_TOPIC"
ITEM_SERVO = 'servo_system'
ITEM_FCC = 'fcc_system'
ITEM_INS = 'sensor_ins'
ITEM_BINS = 'sensor_bins'
ITEM_ADC = 'sensor_adc'
ITEM_ADC2 = 'sensor_adc2'
ITEM_RADAR = 'sensor_radar'
ITEM_GPS = 'sensor_gps'
ITEM_DATALINK = 'datalink'
ITEM_ELEC = 'elec_system'
ITEM_ENGINE = 'engine_system'
ITEM_WARNNING = 'warnning_system'
ITEM_EQUIPMENT = 'equipment'
ITEM_ECHO_CMD = 'command_echo'

# win默认保存数据的地方
port_filename = 'gcs_config.ini'  # 保存链路接口的文件名称
warning_filename = 'alarm_setting.ini'  # 保存报警配置的文件名称
runway_set_filename = 'runway_setting.ini'  # 跑道设置的文件名称

SPEED_V1 = 90
SPEED_V2 = 120
SPEED_V1_TEXT = "V1"
SPEED_V2_TEXT = "V2"



def crc16(strbuf, lenth):
    result = 0
    tempcrc16 = 0
    tempdata = 0
    m = 0
    n = 0
    result = result
    for m in range(lenth):
        result = (result & 0xFFFF)  # 因为Python的int整形数没有最大值，所以需要&上0xffff
        tempcrc16 = (tempcrc16 & 0xFFFF)  # 因为Python的int整形数没有最大值，所以需要&上0xffff
        tempdata = (tempdata & 0xFFFF)  # 因为Python的int整形数没有最大值，所以需要&上0xffff
        tempcrc16 = (((result >> 8) ^ strbuf[m]) & 0xffff)
        tempdata = (tempcrc16 << 8)
        tempcrc16 = 0

        for n in range(8):
            if ((tempdata ^ tempcrc16) & 0x8000):
                tempcrc16 = (((tempcrc16 << 1)) ^ 0x1021)
            else:
                tempcrc16 = (tempcrc16 << 1)
            tempdata = (tempdata << 1)
        # print(tempcrc16)
        result = ((result << 8) ^ tempcrc16)

    return result
